package utility;

import java.awt.Color;

import main.Sprite;
import quadtree.DoublePoint;

public class ContraMan extends Sprite {

	private static final int groundConstant = 600;
	private boolean isJumping = false;
	private boolean isCrouching = false;
	private boolean falling = false;
	private boolean onPlatform = false;
	private boolean fallFarther = false;
	private int platformStart;
	private int platformEnd;
	private int groundY;
	public ContraGun gunOfContraMan;
	
	public ContraMan(int x, int y) {
		super();
		this.x = x;
		this.y = y;
		width = 30;
		height = 50;
		groundY = groundConstant;
		collidable = true;
	}

	public void draw(){
		g.setColor(Color.GREEN);
		this.g.fillRect(this.x, this.y, width, height);        	        
	}

	public void think(){
		x+=dx;
		if(onPlatform) {
			if((x + (width/2) < platformStart || x + (width/2) > platformEnd)
					&& !isJumping) {
				isJumping = true;
				falling = true;
				groundY = groundConstant;
				onPlatform = false;
			}
			else if ((x + (width/2) < platformStart || x + (width/2) > platformEnd)
					&& isJumping) {
				fallFarther = true;
				/*if(falling) {
					groundY = groundConstant;
				}*/
				onPlatform = false;
			}
		}
		gunOfContraMan.setX(x+width/2);
		gunOfContraMan.setY(y + height/2);
		if(isJumping)
		{
			if(Math.abs(y - groundY) < (2*height) && !falling)
			{
				dy = -3;
			}
			else {
				falling = true;
				if(fallFarther) {
					fallFarther = false;
					groundY = groundConstant;
				}
				dy = 2;
			}
			y+=dy;
			//gunOfContraMan.setY(gunOfContraMan.getY() + dy);
			if(y + height >= groundY) {
				y = groundY - height;
				gunOfContraMan.setY(y+height/2);
				isJumping = false;
				falling = false;
				dy = 0;
			}
		}
		if(y+height>=groundConstant) {
			y = groundConstant - height;
		}
	}

	public boolean doesCollide(Sprite s) {
		if(s instanceof ContraPlatform) {
			if(s.getX() <= x + (width/2) && s.getX() + s.getWidth() >= x + (width/2)
					&& s.getY() < y + height && s.getY() + s.getHeight() > y) {
				if(isJumping) {
					if(falling) {
						y = s.getY() - height;
						onPlatform = true;
						platformStart = s.getX();
						platformEnd = s.getX() + s.getWidth();
						jumpInterrupted(true);
						return true;
					}
					else {
						jumpInterrupted(false);
						y = s.getY() + s.getHeight();
						return true;
					}
				}
				else {
					if (dx > 0) {
						setX(s.getX() - getWidth()/2-1);
					}
					else if (dx < 0) {
						int x = s.getX() + s.getWidth() + (getWidth()/2)+1;
						setX(s.getX() + s.getWidth() - (getWidth()/2)+1);
					}
				}
			}
		}
		return false;
	}

	public void keyPressed(String key) {
		if (key == "VK_LEFT")
		{
			dx = -2; 
		}
		if (key == "VK_DOWN")
		{
			height = 25;
			if(!isCrouching) {
				y+=25;
				isCrouching = true;
			}
		}
		if (key == "VK_RIGHT")
		{
			dx = 2; 
		}
		if (key == "VK_SPACE")
		{
			if(!isJumping) {
				groundY = y + height;
				isJumping = true;
			}
		}
	}

	public void keyReleased(String key) {
		if (key == "VK_LEFT" || key == "VK_RIGHT")
		{
			dx = 0; 
		}
		if (key == "VK_DOWN")
		{
			if(isCrouching) {
				height = 50; 
				y -= 25;
			}
			isCrouching = false;
		}
	}

	public void createContraMansGun() {
		gunOfContraMan = new ContraGun(x+width/2, 575);
		gameTree.insertSprite(new DoublePoint(30,550), gunOfContraMan);
	}
	
	public void jumpInterrupted(boolean fallingInterrupted) {
		if(fallingInterrupted) {
			falling = false;
			isJumping = false;
		}
		else {
			falling = true;
			dy = 0;
		}
	}

	public void setOnPlatform(boolean onPlatform, int x, int width) {
		this.onPlatform = onPlatform;
		platformStart = x;
		platformEnd = width;
	}

	public boolean isOnPlatform() {
		return onPlatform;
	}
	
	public boolean isFalling() {
		return falling;
	}
	
	public boolean isJumping() {
		return isJumping;
	}
}
